﻿using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using Hopscotch.Application.Shell.Commands;
using Hopscotch.Presentation.Common;
using Hopscotch.Presentation.Common.Data;
using Hopscotch.Presentation.Common.Threading;

namespace Hopscotch.Application.Shell
{
    public class ShellViewModel : IShellContext
    {
        private static int count;
        public int WorkspaceId { get; private set; }
        public Guid InstanceId { get; private set; }

        private readonly IAppController appController;
        private readonly IDispatcher dispatcher;

        /// <summary>Scoped Region manager</summary>
        /// <remarks>Must be public to bind to view</remarks>
        public IRegionManager RegionManager { get; private set; }

        public ICommand ExitCommand { get; set; }
        public ICommand NewShellCommand { get; set; }
        public ICommand ShowToolCommand { get; set; }

        public ShellViewModel()
        {
        }

        public ShellViewModel(
            IUnityContainer container,
            IRegionManager regionManager,
            IAppController appController)
        {
            WorkspaceId = count++;//Temp, get this number when creating or loading ws
            InstanceId = Guid.NewGuid();

            this.RegionManager = regionManager;
            this.appController = appController;
            this.dispatcher = ApplicationDispatcher.CreateFromCurrentThread();

            //Wire-up commands
            this.ExitCommand = new DelegateCommand(Exit);
            this.NewShellCommand = new CreateShellCommand(container, regionManager, appController);
            this.ShowToolCommand = new LoadToolCommand(container, regionManager, this, dispatcher);

            //Register with the AppController
            appController.RegisterShell(this);
        }

        public ObservableCollection<ToolMenuItem> ToolMenuItems
        {
            get
            {
                return appController.ToolMenuItems;
            }
        }

        public void Close()
        {
            appController.CloseShell(this);
        }

        private void Exit()
        {
            appController.Exit();
        }
    }
}
